<?php
/**
 * Created by PhpStorm.
 * User: geolo
 * Date: 2018/9/21
 * Time: 16:38
 */
namespace app\modules\mch\models;

use app\models\Content;
use yii\data\Pagination;
class ContentForm extends Model
{
    public $wechat_app_id;
    public $title;
    public $title_s;
    public $is_delete;
    public $image;
    public $subject_id;
    public $content;
    public $type;
    public $video;
    public $video_type;
//    public $goods_id;
    public function rules(){

        return [
            [['wechat_app_id'], 'trim'],
            [['wechat_app_id','subject_id'], 'required'],
        ];
    }
    public function attributeLabels()
    {
        return [
            'wechat_app_id' => '请先申请小程序',

        ];
    }
    public function save($id=null)
    {
        if (!$this->validate())
            return $this->getModelError();
        $model = new Content();
        $model->attributes = $this->attributes;
        $model->wechat_app_id = $this->wechat_app_id;
        $model->content = $this->content;
        $model->video = $this->video;
        $model->video_type = $this->video_type;
//        $model->goods_id = $this->goods_id;
        $contents = Content::findOne(['id'=>$id]);
//        var_dump($model->attributes);
        if(!$contents) {
             $res = $model->save();
            if ($res) {
                return [
                    'code' => 0,
                    'msg' => '保存成功',
                    'id'=>$model->id,
                    'url'=>\Yii::$app->urlManager->createUrl(['mch/content/content-edit','id'=>$model->id]),
                ];
            }
        }else{
            $title = $this->title;
            $title_s =$this->title_s ;
            $image =$this->image;
            $content =$this->content;
            $type = $this->type;
            $video = $this->video;
            $video_type = $this->video_type;
//            $goods_id = $this->goods_id;
            if(Content::updateAll(['title'=>$title,'title_s'=>$title_s,'image'=>$image,'content'=>$content,'type'=>$type,'video'=>$video,'video_type'=>$video_type],['id'=>$id])){
                return [
                    'code' => 0,
                    'msg' => '修改成功',
                    'id'=>$id,
                    'url'=>\Yii::$app->urlManager->createUrl(['mch/content/content-edit','id'=>$id]),
                ];
            }

        }
        return $this->getModelError($model);
    }
}